package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.TbShoppingCarts;
import com.ruoyi.system.service.ITbShoppingCartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 购物车Controller
 * 
 * @author ruoyi
 * @date 2024-04-07
 */
@RestController
@RequestMapping("/system/carts")
public class TbShoppingCartsController extends BaseController
{
    @Autowired
    private ITbShoppingCartsService tbShoppingCartsService;

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi('system:carts:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbShoppingCarts tbShoppingCarts)
    {
        startPage();
        List<TbShoppingCarts> list = tbShoppingCartsService.selectTbShoppingCartsList(tbShoppingCarts);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('system:carts:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbShoppingCarts tbShoppingCarts)
    {
        List<TbShoppingCarts> list = tbShoppingCartsService.selectTbShoppingCartsList(tbShoppingCarts);
        ExcelUtil<TbShoppingCarts> util = new ExcelUtil<TbShoppingCarts>(TbShoppingCarts.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:carts:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbShoppingCartsService.selectTbShoppingCartsById(id));
    }

    /**
     * 新增购物车
     */
    @PreAuthorize("@ss.hasPermi('system:carts:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbShoppingCarts tbShoppingCarts)
    {
        return toAjax(tbShoppingCartsService.insertTbShoppingCarts(tbShoppingCarts));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('system:carts:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbShoppingCarts tbShoppingCarts)
    {
        return toAjax(tbShoppingCartsService.updateTbShoppingCarts(tbShoppingCarts));
    }

    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('system:carts:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbShoppingCartsService.deleteTbShoppingCartsByIds(ids));
    }
}
